import React from 'react';
import { Radio } from 'antd';
import classNames from 'classnames';

import Text from './_components/Text';
import styles from './index.less';

const RadioGroup = Radio.Group;

const Index = (props) => {
  const {
    options = [
      { label: '第一', value: 1 },
      { label: '第二', value: 2 },
      { label: '第三', value: 3 },
    ],
    vertical = false,

    disabled,
    value,
    onChange,

    ...rest
  } = props;

  if (!Object.keys(props).includes('value')) {
    return 'DEPRECATED';
  }

  if (disabled && disabled.render) {
    return <Text value={value} options={options} render={disabled.render} />;
  }

  return (
    <RadioGroup
      {...rest}
      disabled={!!disabled}
      value={value}
      onChange={(e) => {
        if (onChange) {
          onChange(e.target.value); // 保持警惕
        }
      }}
    >
      {options.map((item) => (
        <Radio key={item.value} {...item} className={classNames({ [styles.vertical]: vertical })}>
          {item.label}
        </Radio>
      ))}
    </RadioGroup>
  );
};

Object.keys(RadioGroup).forEach((item) => {
  if (!['defaultProps', 'propTypes', 'childContextTypes'].includes(item)) {
    Index[item] = RadioGroup[item];
  }
});
Index.displayName = 'Radio';
export default Index;
